package com.woniu.smart.controller;

import com.woniu.smart.entity.SysAdmin;
import com.woniu.smart.service.SysAdminService;
import com.woniu.smart.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: TODO
 * @Author: WangHeng
 * @Date: 2022/10/19 19:43
 */
@RestController
@RequestMapping("admins")
public class SysAdminController {
    @Autowired
    private SysAdminService sysAdminService;
    @Autowired
    //加密的引入
    private PasswordEncoder passwordEncoder;
//    已切换安全框架登陆
    /*@PostMapping
    public Result login(@RequestBody AdminDTO adminDTO){
        SysAdmin admin = sysAdminService.login(adminDTO);
        if (admin != null) {
            return Result.ok();
        }else {
            return Result.error();
        }
    }*/
    @GetMapping
    public Result queryAll(){
        List<SysAdmin> sysAdmins = sysAdminService.selectByAll();
        if (sysAdmins.size() > 0) {
            return Result.ok().data("sysAdmins",sysAdmins);
        }else {
            return Result.error();
        }
    }
    @GetMapping("{id}")
    public Result queryById(@PathVariable("id") Long id){
        SysAdmin sysAdmin = sysAdminService.selectByPrimaryKey(id);
        if (sysAdmin != null) {
            return Result.ok().data("sysAdmin",sysAdmin);
        }else {
            return Result.error();
        }
    }
    @PostMapping
    public Result insert(@RequestBody SysAdmin adminDTO){
        //加密处理
        adminDTO.setEmpPawd(passwordEncoder.encode(adminDTO.getEmpPawd()));
        int i = sysAdminService.insertSelective(adminDTO);
        if (i != -1) {
            return Result.ok();
        }else {
            return Result.error();
        }
    }
}
